//
//  PicDetialViewController.m
//  News
//
//  Created by xalo on 16/3/1.
//  Copyright © 2016年 News. All rights reserved.
//

#import "PicDetialViewController.h"
#import "HTTPHelper.h"
#import "DetialModel.h"
#import "ScrollPageView.h"
#import <UIImageView+WebCache.h>
#import "FMDatabase+Database.h"
#import "UMSocial.h"
#import "CommentViewController.h"
#import <Reachability.h>
#import "LoginHelp.h"
#import "PopupController.h"
#import "UIViewController+Notice.h"
#import "CollectViewController.h"
#import "ShowViewController.h"


@interface PicDetialViewController () <UIScrollViewDelegate>

//pinglun geshu
@property (strong, nonatomic) IBOutlet UIBarButtonItem *CommentCount;

@property (nonatomic ,strong) NSMutableArray *tableArray;

@end

@implementation PicDetialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
      //加载数据
    [self getCache];
    
     //一键换肤
    [self changeBackgroung];
}

//返回
- (IBAction)backView:(id)sender {
    
     [self dismissViewControllerAnimated:YES completion:nil];
}


// 分享与收藏
- (IBAction)comment:(id)sender {
    
//    CommentViewController *comment = [[UIStoryboard storyboardWithName:@"Picture" bundle:nil] instantiateViewControllerWithIdentifier:@"comment"];
//    
//    UINavigationController *na = [[UINavigationController alloc]initWithRootViewController:comment];
//    
//    comment.comments = self.commentsLS;
//    comment.time = 1;
//    
//    [self presentViewController:na animated:YES completion:nil];
    

    
    
    CollectViewController *collect = [[UIStoryboard storyboardWithName:@"Collect" bundle:nil] instantiateViewControllerWithIdentifier:@"collect"];
    
    //传给收藏所需要的属性
    NSDictionary *dic = [NSDictionary dictionary];
    dic = @{@"comments":self.comments,@"link":self.link};
    
    [self.tableArray addObject: dic];

    collect.picArray = self.tableArray;
    collect.storageID = self.comments;
    

    
    collect.pictureLink = self.picLink; // 分享图片链接
    collect.titShare = self.link ;   //标题链接
    
    [self.view addSubview:collect.view];
    [self addChildViewController:collect];

}


//判断网络
-(void)getCache {
    
    if ([self isInternet]) {
        
        [[HTTPHelper sharedManger] entDetailsInterfaceWithNumber:self.comments picArray:self.tableArray block:^{
            
            [self scollPage];
            
        }];
        
    } else {
        
        NSArray *array = [[FMDatabase shareManager] selectNewsModelArrayWithChannel:self.UL times:1];
        
        for (NSDictionary *dic in array) {
            
            DetialModel *list = [[DetialModel alloc]init];
            
            [list setValuesForKeysWithDictionary:dic];
            
            [self.tableArray addObject:list];
        }
    }
}

//判断网络
-(BOOL)isInternet {
    
    BOOL state;
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    if ([reach currentReachabilityStatus]) {
        
        return state = YES;
    }
    
    return state = NO;
}

//手写轮播图
-(void)scollPage {
    
    //轮播图的个数
    NSInteger count =self.tableArray.count  ;
    
      // 轮播的宽度
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    
    UIScrollView *scollView = [[UIScrollView alloc]initWithFrame:
                               CGRectMake(0, 128 ,width, height )];
    //屏幕互动的宽度
    scollView.contentSize =CGSizeMake(width * count, 0);
    //整夜滑动
    scollView.pagingEnabled =YES;
    scollView.showsVerticalScrollIndicator = NO;
    
    
    for (NSInteger i = 0; i < self.tableArray.count ; i++) {
        
        self.model = self.tableArray[i];
        
         self.picture =[[UIImageView alloc]initWithFrame:CGRectMake(width * i + 10, 0 ,width -20, height -288 )];
        
        [self.picture sd_setImageWithURL:[NSURL URLWithString:self.model.kpic]];
//        self.picture.contentMode = UIViewContentModeScaleAspectFit;
        
        
        UILabel *count = [[UILabel alloc]initWithFrame:CGRectMake(width - 80, height - 318, 60, 30)];
        count.text = [NSString stringWithFormat:@"%ld/%ld",i+1 ,self.tableArray.count];
        count.textColor = [UIColor whiteColor];
        
          [self.picture addSubview:count];
        
        //判断是否有图片
        if (self.picture == nil) {
            
        }
        else {
        
        //创建过度动画
        CATransition *transition = [CATransition animation];
        
        //私有的: @"rippleEffect" , @"oglFlip" , @"cube";
        //设置类型
        transition.type = @"rippleEffect";
        
        //设置过度动画方向
        transition.subtype =kCATransitionFromRight;
        transition.duration = 2;
        [self.view.layer addAnimation:transition forKey:@"guodu"];
            
        }

    
    


        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(width * i +10, height - 278, width -20, 20)];
        title.text = self.link;
        
        UILabel *subTitle = [[UILabel alloc]initWithFrame:CGRectMake(width *i +10, height - 258, width - 20 , 80)];
        subTitle.text =self.model.alt;
        subTitle.numberOfLines =0 ;
        subTitle.font = [UIFont systemFontOfSize:13];
        
        [scollView addSubview:title];
        [scollView addSubview:self.picture];
        [scollView addSubview:subTitle];
        [self.view addSubview:scollView];
       
      }
   
}

//懒加载
-(NSMutableArray *)tableArray {
    
    if (!_tableArray) {
        _tableArray = [NSMutableArray array];
    }
    
    return _tableArray;
}

//通知
-(void)changeBackgroung
{
    
    extern BOOL isNightMode;
    if (isNightMode) {
        
        self.view.backgroundColor = [UIColor darkGrayColor];
        
    }else {
        
        self.view.backgroundColor = [UIColor whiteColor];
    }
    
}

//不让横屏,和下面方法
- (BOOL)shouldAutorotate {
    return YES;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations {
    
    return UIInterfaceOrientationMaskPortrait;
}

@end
